/**
 * Created by isoft-work 5 on 12/24/2014.
 */

$(document).ready(function(){
//   render
   renderUserCheckBill();
//   event
});

function renderUserCheckBill(){
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   if(!useName) {
      ISS.WINDOW.location('/jsp/user/user-login.jsp');
   }
   else {
      getAllBillUser(useName);
   }
}

function getAllBillUser(useName) {
   var user = {};
   user.maTV = useName;
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/EditAccount/getAllBill/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(user),
      success: function (data) {
         var responseInfo = data.responseObject;
         getAllBillUserComplete(responseInfo);
      }
   });
}

function getAllBillUserComplete(bill) {
   var html = '';
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   generaDataBill(bill);
   for(var i = 0; i < bill.length; i++) {
      bill[i].href = ISS.SESSION.getSession('contextPath') + '/service/EditAccount/toDetailBill/' + bill[i].maHD + '/' + useName + '.';
      html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.CheckBill.listBill, bill[i]);
      delete bill[i].href;
   }
   ISS.DOM.html('#list_Bill', html);

}

function generaDataBill(bill) {
   for(var i = 0; i < bill.length; i++) {
      bill[i].ngayLapHD = bill[i].ngayLapHD ? ISS.DATE.getFullDate(new Date(bill[i].ngayLapHD)) : '';
      bill[i].ngayGiao = bill[i].ngayGiao ? ISS.DATE.getFullDate(new Date(bill[i].ngayGiao)) : '';
      bill[i].ngayNhan = bill[i].ngayNhan ? ISS.DATE.getFullDate(new Date(bill[i].ngayNhan)) : '';
      bill[i].tongTien = ISS.STRING_UTIL.formatNumeral(bill[i].tongTien, '0,0');
      if(bill[i].thanhToan == 0) {
         bill[i].thanhToan = 'Chưa thanh toán';
      }
      else {
         bill[i].thanhToan = 'Đã thanh toán';
      }

      if(bill[i].trangThai == 0) {
         bill[i].trangThai = 'Đang xử lý';
      }
      else if (bill[i].trangThai == 1) {
         bill[i].trangThai = 'Đang giao hàng';
      }
      else if (bill[i].trangThai == 2) {
         bill[i].trangThai = 'Hoàn thành';
      }
      else if (bill[i].trangThai == 3) {
         bill[i].trangThai = 'Đã huỷ';
      }
   }
}
